package com.taxsystem.chengwenzhi.sscx;

import java.util.HashMap;
import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActionBar;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.main.taxsystem.R;
import com.taxsystem.tool.HttpUtil;

public class DjxxcxActivity extends Activity {
	private static final String TAG ="DjxxcxActivity"; 
	private static final String ACTION = "com.taxsystem.chengwenzhi.sscx.HTTP_GET_RESPONSE";
	private LocalBroadcastManager lbm = null;
	private String url = null;
	private Thread myThread = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_djxxcx);
		
		url = UrlBuilder.getUrlBuilder().toString();
		registerBroadcastReceiver();
		
		ActionBar bar = getActionBar();
        bar.setHomeButtonEnabled(true);
        bar.setIcon(R.drawable.actionbar_icon);
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setBackgroundDrawable(new ColorDrawable(0xff34a4f7));
//		if (CommonOperations.isNetworkConnected(this)) {
//			myThread = new MyThread();
//			myThread.run();
//		}else
//			Log.w(TAG,"device can't connect network");
	}
	
	void registerBroadcastReceiver() {
		lbm = LocalBroadcastManager.getInstance(this);
		IntentFilter filter = new IntentFilter(ACTION);
		BroadcastReceiver myRecive = new MyRecive();
		lbm.registerReceiver(myRecive, filter);
	}

	private static class UrlBuilder{
		public static final String PATH = "/app/taxservice/taxregisterinfo";
		private static UrlBuilder instance = new UrlBuilder();
		public HashMap<String,String> params = null;
		public static final String TAX_PAYER_CODE = "taxpayercode";
		public static final String TAX_PAYER_NAME = "taxpayername";
		public static final String SWDJH = "swdjh";
		
		 private UrlBuilder(){
			params = new HashMap<String,String>();
			params.put(TAX_PAYER_CODE, "1111	");
			params.put(TAX_PAYER_NAME, "张三");
			params.put(SWDJH, "22222222222");
		}
		 
		public void setTaxPayerCode(String taxPayerCode){
			params.put(TAX_PAYER_CODE,taxPayerCode);
		}
		public void setTaxPayerName(String taxPayerName){
			params.put(TAX_PAYER_NAME, taxPayerName);
		}
		public void setSWDJH(String swdjh){
			params.put(SWDJH, swdjh);
		}
		public String getTaxPayerCode(){
			return params.get(TAX_PAYER_CODE);
		}
		public String getTaxPayerName(){
			return params.get(TAX_PAYER_NAME);
		}
		public String getSwdjh(){
			return params.get(SWDJH);
		}

		public static UrlBuilder getUrlBuilder(){
			return instance;
		}

		@Override
		public String toString() {
			return PATH+'?'
					+TAX_PAYER_CODE+'='+params.get(TAX_PAYER_CODE)
					+'&'+TAX_PAYER_NAME+'='+params.get(TAX_PAYER_NAME)
					+'&'+SWDJH+'='+params.get(SWDJH);
		}
	}
	
	private class MyThread extends Thread {
		@Override
		public void run() {
			Log.v(TAG, "MyThread.run():start");
			Intent intent = new Intent(ACTION);
			try {
				String result = HttpUtil.getRequest(url);
				Log.v(TAG, "get data from HttpUtil:" + result);
				intent.putExtra("JSON", result);
				lbm.sendBroadcast(intent);
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (ExecutionException e) {
				e.printStackTrace();
			}
		}
	}
	
	private class MyRecive extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			Log.v(TAG, "MyRecive.onReceive():recive a broadcast");
			String jsonString = intent.getStringExtra("JSON");

			if (jsonString == null) {
				Log.e(TAG, "MyRecive.onReceive():jsonString is null");
				return;
			}

			try {
				JSONObject jsonObject = new JSONObject(jsonString);
				JSONArray data = jsonObject.getJSONArray("data");
			} catch (JSONException e) {
				e.printStackTrace();
			} finally {
			}
		}
	}
}
